<?php
/**
 * TINPHP核心库
 * TINPHP - A PHP Framework
 * @author   WU SHAIN <634976171@QQ.com>
 */

//定义网站根目录,JS,CSS,IMG 调用地址
$spath=rtrim(substr(dirname(str_replace("\\", '/', dirname(__FILE__))), strlen(rtrim($_SERVER["DOCUMENT_ROOT"],"/\\"))), '/\\');
$GLOBALS["root"]=$spath.'/'; //Web服务器根到项目的根
define("URL_ROOT", rtrim($GLOBALS["root"], '/'));

/*站点跟网址*/
define('SITE_URL',$_SERVER['SCRIPT_NAME']);
/*项目根目录*/
define('ROOT_PATH','./');
define('SYSPLUGINS_DIR','./libs/sysplugins/');
require ROOT_PATH.'configs/database.php';
require ROOT_PATH.'configs/app.php';
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set(DEFAULT_TIMEZONE);



//通过URL分离控制器和方法
function detect_uri() {
    // $_SERVER['REQUEST_URI']: 访问此页面所需的URI
    // $_SERVER["SCRIPT_NAME"]: 当前脚本的路径
    if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME'])) {
        return '';
    }
//    $sname=strstr($_SERVER['SCRIPT_NAME'], 'index.php')?str_replace ( 'index.php','',$_SERVER['SCRIPT_NAME'] ):$_SERVER['SCRIPT_NAME'];
    $sname=$_SERVER['SCRIPT_NAME'];
    $uri = $_SERVER['REQUEST_URI'];
    if (strpos($uri, $sname) === 0) {
        //去掉共同部分，取得对路由有用的部分。
        $uri = substr($uri, strlen($sname));
    }
    //任何路由信息的传递，可以直接返回
    if ($uri == '/' || empty($uri)) {
        return 'index/index';
    }
    // 如果 strpos($uri, $_SERVER['SCRIPT_NAME']) === 0和elseif都无法匹配的时候，
    // 返回这个url的path部分。
    $uri = parse_url($uri, PHP_URL_PATH);
    // 将路径中的 '//' 或 '../' 等进行清理
    return str_replace(array('//', '../'), '/', trim($uri, '/'));
}
$url=explode('/',detect_uri());

if(count($url)==1){
    $controller=$url[0].'Controller';
    $method_name ='index';
}else{
    $controller=$url[0].'Controller';
    $method_name =$url[1];
}

/*如果控制器不存在*/
if(!file_exists(APP_PATH.$controller.'.php')){
    $controller='errorController';
    $method_name ='index';
}

//加载核心类
//设置包含目录（类所在的全部目录）,  PATH_SEPARATOR 分隔符号 Linux(:) Windows(;)
function initialize_include()
{
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."core/");
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."extends/");
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."libs/");
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."app/controllers/");
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."libs/sysplugins");
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."libs/plugins");
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH."cache/");
    //自动加载所在项目控制器
    set_include_path(get_include_path().PATH_SEPARATOR . APP_PATH);
    set_include_path(get_include_path().PATH_SEPARATOR . ROOT_PATH.'controllers/');
    set_include_path(get_include_path().PATH_SEPARATOR . APP_PATH.'models/');
}
initialize_include();
function __autoload($className){
    if($className=='Smarty'){
        require "Smarty.class.php";
    }else{
        require $className.'.php';
    }
}

//开启SESSION
Tsession::start();

//调用控制器方法
$c=new $controller();

if(!method_exists($c,$method_name)){
    $controller='errorController';
    $c=new $controller();
    $c->index();
}else{
    $c->$method_name();
}

